<?php
namespace Home\Controller;

use Utuo\DistributionShopForeground;
use My\Model\AgentModel;
use My\Model\DistributionShopOrderModel;

class DisShopAgentController extends DistributionShopForeground {
	
	/**
	 * 初始化
	 */
	public function _init() {
		layout('disshop');
		$this->setModelName("DistributionShop");
		$this->setModelControllerName("DisShop");
		if ( in_array(ACTION_NAME, array('apply', 'search')) ) { // 支持跳转获得用户信息
			$this->setSupportRedirect(isset($_REQUEST['debug'])?false:true);
		}
	}
	
	/**
	 * 代理商个人中心
	 */
	public function myAction() {
		$this->setPageTitle('用户中心');
		// 订单导航
		$this->getOrderNav();
		$this->display("{$this->modelControllerName}/agentMy");
	}
	
	/**
	 * 订单导航
	 */
	private function getOrderNav() {
		$types = array();
		foreach (DistributionShopOrderModel::$TYPES as $type => $v) {
			$d = M("Order orders");
			$condition = array(
					"orders.uid" => $this->uid,
					"orders.cid" => $this->sid
			);
			if ($type == 1) { // 未付款
				$condition['orders.order_status'] = DistributionShopOrderModel::ORDER_STATUS_PAY;
			} else if ($type == 2) { // 待签收(已付款,货到付款)
				$condition['orders.order_status'] = array(
						"in",
						array(
								DistributionShopOrderModel::ORDER_STATUS_SHIPPING,
								DistributionShopOrderModel::ORDER_STATUS_SIGN
						)
				);
			} else if ($type == 3) { // 交易成功
				$condition['orders.order_status'] = DistributionShopOrderModel::ORDER_STATUS_COMPLETED;
			} else if ($type == 4) { // 退款订单
				$condition['orders.refund_times'] = array('gt', 0);
			} else { // 其它
				$condition['orders.order_status'] = array(
						"not in",
						array(
								DistributionShopOrderModel::ORDER_STATUS_PAY,
								DistributionShopOrderModel::ORDER_STATUS_SHIPPING,
								DistributionShopOrderModel::ORDER_STATUS_SIGN,
								DistributionShopOrderModel::ORDER_STATUS_COMPLETED
						)
				);
			}
			$count = $d->where($condition)->count();
			$types[$type] = $v . "($count)";
		}
		$this->assign("types", $types);
	}
	
	
	/**
	 * 申请代理商
	 */
	public function applyAction() {
		$this->setPageTitle("代理商在线申请");
		$this->display("{$this->modelControllerName}/agentApply");
	}
		
	/**
	 * 提交申请
	 */
	public function applyPostAction() {
		$d = D("AgentApply");
		if ( $d->create() ) {
			$d->cid = $this->cid or $this->ajaxError('未知商家');
			$d->openId = $this->openId or $this->ajaxError('访问已超时请重新进入');
			if ( false !== $d->add() ) {
				$this->ajaxSuccess('您的申请已提交,我们会尽快与您联系');
			}
			$this->ajaxError('提交失败请重试');
		}
		$this->ajaxError($d->getError());
	}
	
	/**
	 * 查询已申请的代理商
	 */
	public function searchAction() {
		$this->setPageTitle("代理商官方授权查询");
		$this->display("{$this->modelControllerName}/agentSearch");
	}
	
	/**
	 * 查询已申请的代理商
	 */
	public function doSearchAction() {
		$this->cid or $this->ajaxError('未知商家');
		//$this->openId or $this->ajaxError('访问已超时请重新进入');
		$tel = I('code', 0) or $this->ajaxError('请输入查询手机');
		$agent = M("Agent")->where(array('cid'=>$this->cid, 'tel'=>$tel))->field('name,certificateImg,level')->find();
		$agent and $agent['level'] = AgentModel::$agentLevel[$agent['level']];
		if ( $agent ) {
			$this->ajaxSuccess('', $agent);
		} else {
			$this->ajaxError('您查询的代理商不存在');
		}
	}
	
}